/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * PanelReceipt.java
 *
 * Created on Feb 14, 2012, 11:32:05 PM
 */
package GUI;

import Model.ReceiptTableModel;
import Entity.ConnectionSQL;
import Entity.Receipt;
import Entity.User;
import java.util.List;
import java.util.ResourceBundle;
import javax.swing.JOptionPane;

/**
 *
 * @author Duc
 */
public class PanelReceipt extends javax.swing.JPanel {
    private ReceiptTableModel receiptTableModel;
    private ResourceBundle messages;
    private User user;
    /** Creates new form PanelReceipt */
    public PanelReceipt(ResourceBundle messages,User user) {
        initComponents();
        receiptTable.getTableHeader().setReorderingAllowed(false);
        receiptTable.setAutoCreateRowSorter(true);
        this.messages = messages;
        this.user =user;
        viewAllReceipt();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel4 = new javax.swing.JPanel();
        jScrollPane3 = new javax.swing.JScrollPane();
        receiptTable = new javax.swing.JTable();
        jButton1 = new javax.swing.JButton();
        btnEdit = new javax.swing.JButton();

        receiptTable.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        receiptTable.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                receiptTableMouseClicked(evt);
            }
        });
        jScrollPane3.setViewportView(receiptTable);

        jButton1.setText("New Receipt");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        btnEdit.setText("Edit Receipt");
        btnEdit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnEditActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
        jPanel4.setLayout(jPanel4Layout);
        jPanel4Layout.setHorizontalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addGap(110, 110, 110)
                .addComponent(jButton1)
                .addGap(64, 64, 64)
                .addComponent(btnEdit)
                .addContainerGap(133, Short.MAX_VALUE))
            .addComponent(jScrollPane3, javax.swing.GroupLayout.DEFAULT_SIZE, 489, Short.MAX_VALUE)
        );
        jPanel4Layout.setVerticalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 352, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton1)
                    .addComponent(btnEdit))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed

        newReceipt();     }//GEN-LAST:event_jButton1ActionPerformed

    private void btnEditActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnEditActionPerformed
        editReceipt();
    }//GEN-LAST:event_btnEditActionPerformed

    private void receiptTableMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_receiptTableMouseClicked
        if(evt.getClickCount()==2) editReceipt();
    }//GEN-LAST:event_receiptTableMouseClicked

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnEdit;
    private javax.swing.JButton jButton1;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JTable receiptTable;
    // End of variables declaration//GEN-END:variables
    public void viewAllReceipt(){
        ConnectionSQL cnn = new ConnectionSQL(messages);
        List<Receipt> listReceipts = cnn.getReceiptByUser(user.getId());
        receiptTableModel = new ReceiptTableModel(listReceipts, messages);
        receiptTable.setModel(receiptTableModel);
        receiptTableModel.fireTableDataChanged();
    }
    private void newReceipt() {
        DialogAddReceipt dar = new DialogAddReceipt(this, true, user, messages);
        dar.setVisible(true);
        viewAllReceipt();
    }
    private void editReceipt(){
        int select  =receiptTable.getSelectedRow();
        if(select ==-1 ) JOptionPane.showMessageDialog(this, messages.getString("messagesNoSelect"));
        else{
            ConnectionSQL cnn = new ConnectionSQL(messages);
            int billID = Integer.parseInt(receiptTable.getValueAt(select, 0).toString());
            String itemName = receiptTable.getValueAt(select, 2).toString();
            int quantity = Integer.parseInt(receiptTable.getValueAt(select, 4).toString());
            new DialogEditReceipt(this, true, user, messages, billID, itemName, quantity).setVisible(true);
            receiptTableModel.fireTableDataChanged();
        }
    }
}
